package com.fzdy.dataplatform.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fzdy.dataplatform.controller.api.DelArr;
import com.fzdy.dataplatform.controller.api.ModuleCallback;
import com.fzdy.dataplatform.controller.api.ktv.KtvListApi;
import com.fzdy.dataplatform.controller.api.sysLog.LogStringUtil;
import com.fzdy.dataplatform.controller.api.sysLog.SysLogApi;
import com.fzdy.dataplatform.core.common.AjaxCallback;
import com.fzdy.dataplatform.core.common.Check;
import com.fzdy.dataplatform.core.controller.BaseController;
import com.fzdy.dataplatform.core.vo.Page;
import com.fzdy.dataplatform.entity.KTV;
import com.fzdy.dataplatform.entity.User;
import com.fzdy.dataplatform.service.KTVService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/sysmanage/protocoldictionary")
public class ProtocoldictionaryController extends BaseController {

    @Autowired
    private KTVService ktvService;

    @RequestMapping(value = "/list")
    public String list(@RequestBody Page<KTV> page, HttpServletRequest request) {
        KTV ktv = new KTV();
        ktv.setName(page.getName());
        if (!webUtilify.isAdmin(request)) {
            ktv.setUserid(webUtilify.LoginUserinfo(request).getId());
        }
        ktvService.getPageByExampleLike(ktv, page);
        AjaxCallback ajaxCallback = new AjaxCallback(ktvdbtoapi(page.getList()), page.getTotalRow());
        return JSON.toJSONString(ajaxCallback);
    }


    @RequestMapping(value = "/getktv")
    public String getktv(@RequestBody KTV ktv, HttpServletRequest request) {
        ktv = ktvService.get(ktv.getId());
        KtvListApi api = new KtvListApi(ktv.getId(), reNameKTV(ktv.getName()), ktv.getK(), ktv.getTid(), ktv.getMemo(), ktv.gettType()
                .getTypedescription());
        AjaxCallback ajaxCallback = new AjaxCallback(ModuleCallback.SUCCESSNO, "请求成功");
        ajaxCallback.setData(api);
        System.out.println(JSON.toJSONString(ajaxCallback));
        return JSON.toJSONString(ajaxCallback);

    }

    @RequestMapping(value = "/del")
    public String del(@RequestBody DelArr delArr, HttpServletRequest request) {
        AjaxCallback ajaxCallback = null;
        SysLogApi log_sysLogApi = null;
        JSONObject log_content = new JSONObject();
        boolean flag = true;
        if (!webUtilify.isAdmin(request)) {
            Integer[] ids = delArr.getIds();
            for (Integer id : ids) {
                if (id < 70 || id > 150) {
                    flag = false;
                    ajaxCallback = new AjaxCallback(ModuleCallback.FAILURENO, "普通用户的Key值必须在70-150之间");
                    log_sysLogApi.setStatus(LogStringUtil.FAILURE);
                    log_content.put(LogStringUtil.CAUSEFAIL, "普通用户的Key值必须在70-150之间");
                    break;
                }
            }
        }
        if (flag) {
            ktvService.batchDelete(delArr.getIds());
            log_content.put("id", JSON.toJSONString(delArr.getIds()));
            log_sysLogApi = new SysLogApi(LogStringUtil.DEL, LogStringUtil.PROTOCOLDICTIONARY,
                    LogStringUtil.SUCCESS, log_content, webUtilify.LoginUserinfo(request).getId());
            syslogService.WriteLog(log_sysLogApi);
            ajaxCallback = new AjaxCallback(ModuleCallback.SUCCESSNO, "删除成功");
        }
        return JSON.toJSONString(ajaxCallback);

    }

    @RequestMapping(value = "/edit")
    public String edit(@RequestBody KtvListApi api, HttpServletRequest request) {
        AjaxCallback ajaxCallback;
        User user = webUtilify.LoginUserinfo(request);
        /** 日志*/
        SysLogApi log_sysLogApi = new SysLogApi(LogStringUtil.PROTOCOLDICTIONARY, LogStringUtil.SUCCESS, user.getId());
        JSONObject log_content = new JSONObject();
        if (!webUtilify.isAdmin(request)){
            api.setName("("+user.getName()+")"+api.getName());
        }

        if (!webUtilify.isAdmin(request) && (api.getK() < 70 || api.getK() > 150)) {
            ajaxCallback = new AjaxCallback(ModuleCallback.FAILURENO, "普通用户的Key值必须在70-150之间");
            log_sysLogApi.setStatus(LogStringUtil.FAILURE);
            log_content.put(LogStringUtil.CAUSEFAIL, "普通用户的Key值必须在70-150之间");
        } else {
            if (Check.checkObjNull(api.getK())) {
                ajaxCallback = new AjaxCallback(ModuleCallback.FAILURENO, "Key值不能为空");
                log_sysLogApi.setStatus(LogStringUtil.FAILURE);
                log_content.put(LogStringUtil.CAUSEFAIL, "Key值不能为空");
            } else if (Check.checkObjNull(api.getT())) {
                ajaxCallback = new AjaxCallback(ModuleCallback.FAILURENO, "类型不能为空");
                log_sysLogApi.setStatus(LogStringUtil.FAILURE);
                log_content.put(LogStringUtil.CAUSEFAIL, "类型不能为空");
            } else if (Check.checkObjNull(api.getName())) {
                ajaxCallback = new AjaxCallback(ModuleCallback.FAILURENO, "名称不能为空");
                log_sysLogApi.setStatus(LogStringUtil.FAILURE);
                log_content.put(LogStringUtil.CAUSEFAIL, "名称不能为空");
            } else {
                log_sysLogApi.setOperation(LogStringUtil.UPDATA);
                if (api.getId() != null) {
                    KTV ktv = ktvService.get(api.getId());
                    if (ktv == null) {
                        ajaxCallback = new AjaxCallback(ModuleCallback.FAILURENO, "修改失败，找不到对应的私有协议字典");
                        log_sysLogApi.setStatus(LogStringUtil.FAILURE);
                        log_content.put(LogStringUtil.CAUSEFAIL, "修改失败，找不到对应的私有协议字典");
                    } else {
                        log_content.put(LogStringUtil.BEFOREEDIT, JSON.toJSONString(ktv));
                        ktv.setTid(api.getT());
                        ktv.setName(api.getName());
                        ktv.setMemo(api.getMemo());
                        ktv.setK(api.getK());
                        ktvService.updateT(ktv);
                        log_content.put(LogStringUtil.AFTEREDIT, JSON.toJSONString(ktv));
                        ajaxCallback = new AjaxCallback(true, ModuleCallback.SUCCESSNO, "修改成功");
                    }
                } else {
                    log_sysLogApi.setOperation(LogStringUtil.ADD);
                    KTV ktv = new KTV(api.getName(), api.getK(), api.getMemo(), new Date(), api.getT());
                    ktv.setShare(1);
                    if (webUtilify.isAdmin(request)) {
                        ktv.setShare(1);
                    }else {
                        ktv.setShare(0);
                        ktv.setUserid(user.getId());
                    }
                    ktvService.insertT(ktv);
                    log_content.put("KTV信息", JSON.toJSONString(ktv));
                    ajaxCallback = new AjaxCallback(true, ModuleCallback.SUCCESSNO, "添加成功");
                }
            }
        }
        log_sysLogApi.setContent(log_content);
        syslogService.WriteLog(log_sysLogApi);
        return JSON.toJSONString(ajaxCallback);
    }


    private String reNameKTV(String s){
        if (s.contains("(")&&s.contains(")")){
            int end=s.indexOf(")");
            s=s.substring(end+1,s.length());
        }
       return s;
    }
    public static List ktvdbtoapi(List<KTV> list) {
        List<KtvListApi> apis = new ArrayList<>();
        for (KTV ktv : list) {
            KtvListApi api = new KtvListApi(ktv.getId(), ktv.getName(), ktv.getK(), ktv.getTid(), ktv.getMemo(),
                    ktv.gettType().getTypedescription());
            apis.add(api);
        }
        return apis;
    }


}
